package com.flyee.viewer.menu
{
	import com.flyee.events.SelectionEvent;
	import com.flyee.utils.MyLog;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class OptionCell extends Sprite
	{
		public const WIDTH_CELL:int = 66;
		public const HEIGHT_CELL:int = 32;
		/**
		 * 提示信息 
		 */		
		private var cellTip:String = null;
		/**
		 * 显示提示信息 
		 */		
		private var tipTxt:TextField = null;
		/**
		 * 选中后的背景效果 
		 */		
		private var selectBG:Bitmap = null;
		private var isIcon:Boolean = false;
		private var cellId:int = 0;
		private var cellKeyword:String = null;
		private var position:Point = null;
		private var pos:int = 0;
		/**
		 * 放按钮的容器 
		 */		
		private var iconSprite:Sprite = null;
		private	var tipSprite:Sprite = null;
		private var filterList:Array = null;
		private var hasChild:Boolean = false;
		
		public function OptionCell(id:int,p:int,key:String,isicon:Boolean = false)
		{
			super();
			cellId = id;
			isIcon = isicon;
			pos = p;
			cellKeyword = key;
			//drawBG();
			init();
		}
		
		private function init():void{
			filterList = new Array();
			filterList.push(getGlowFilter());
			filterList.push(getDropShadowFilter());
			
			if(isIcon){
				iconSprite = new Sprite();
				iconSprite.buttonMode = true;
				iconSprite.useHandCursor = true;
				iconSprite.addEventListener(MouseEvent.CLICK,clickHandler);
				iconSprite.y = -6;
				this.addChild(iconSprite);
			}else{
				tipSprite = new Sprite();
				tipTxt = createTxt("");
				selectBG = new Bitmap();
				selectBG.x = -6.5;
				selectBG.y = 2;
				tipSprite.addChild(tipTxt);
				tipSprite.addEventListener(MouseEvent.CLICK,clickHandler);
				tipSprite.y = 7;
				this.addChild(selectBG);
				this.addChild(tipSprite);
			}
			setPos();
		}
		
		private function createTxt(mess:String = ""):TextField{
			var txt:TextField = new TextField();
			var format:TextFormat = new TextFormat("宋体",12,0xFFFFFF);
			txt.defaultTextFormat = format;
			txt.text = mess;
			txt.selectable = false;
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.background = false;
			
			return txt;
		}
		
		private function clickHandler(e:MouseEvent):void{
			if(isIcon){
				
			}else{
				if(cellId < 10){
					var format:TextFormat = new TextFormat("宋体",12,0x044D28);
					tipTxt.defaultTextFormat = format;	
				}
				tipTxt.filters = filterList;
				tipTxt.text = cellTip;
			}
			this.dispatchEvent(new SelectionEvent(SelectionEvent.SELECTED,cellId.toString(),"main"));
		}
		
		private function getDropShadowFilter():BitmapFilter {
            var color:Number = 0xB9FFA2;
            var angle:Number = 90;
            var alpha:Number = 1;
            var blurX:Number = 0;
            var blurY:Number = 0;
            var distance:Number = 1;
            var strength:Number = 0.5;
            var inner:Boolean = false;
            var knockout:Boolean = false;
            var quality:Number = BitmapFilterQuality.HIGH;
            return new DropShadowFilter(distance,
                                        angle,
                                        color,
                                        alpha,
                                        blurX,
                                        blurY,
                                        strength,
                                        quality,
                                        inner,
                                        knockout);
        }
        
        private function getGlowFilter():BitmapFilter {
            var color:Number = 0xC7FC65;
            var alpha:Number = 1;
            var blurX:Number = 0;
            var blurY:Number = 0;
            var strength:Number = 65;
            var inner:Boolean = false;
            var knockout:Boolean = false;
            var quality:Number = BitmapFilterQuality.HIGH;

            return new GlowFilter(color,
                                  alpha,
                                  blurX,
                                  blurY,
                                  strength,
                                  quality,
                                  inner,
                                  knockout);
        }

		
		private function drawBG():void{
			this.graphics.beginFill(0xffff00,0);
			this.graphics.drawRect(0,0,WIDTH_CELL,HEIGHT_CELL);
			this.graphics.endFill();
		}
		
		private function setPos():void{
			position = new Point();
			position.x = pos * WIDTH_CELL;
			position.y = 0;
			this.x = position.x;
			this.y = position.y;
		}
		
		public function setIconButton(btn:SimpleButton):void{
			iconSprite.addChild(btn);
			iconSprite.x = (WIDTH_CELL - iconSprite.width)/2;
			iconSprite.graphics.beginFill(0xff0000,0);
			iconSprite.graphics.drawRect(0,0,iconSprite.width,iconSprite.height);
			iconSprite.graphics.endFill();
		}
		
		public function setTipInfo(mess:String):void{
			cellTip = mess;
			
			if(cellId > 10){
				var format:TextFormat = new TextFormat("宋体",12,0x044D28);
				tipTxt.defaultTextFormat = format;
				tipTxt.filters = filterList;
			}
			tipTxt.text = mess;
			tipSprite.x = (WIDTH_CELL - tipTxt.width)/2;
			tipSprite.y = (HEIGHT_CELL - tipTxt.height)/2+3;
			tipSprite.graphics.beginFill(0x0000ff,0);
			tipSprite.graphics.drawRect(0,0,WIDTH_CELL,HEIGHT_CELL);
			tipSprite.graphics.endFill();
		}
		
		public function get cellID():int{
			return cellId;
		}
		
		public function get isIconCell():Boolean{
			return isIcon;
		}
		
		public function get cellKeywords():String{
			return cellKeyword;
		}
		
		public function set hasChildren(b:Boolean):void{
			hasChild = b;
		}
		
		public function get hasChildren():Boolean{
			return hasChild;
		}
		
		public function showSelectBG(data:BitmapData = null):void{
			if(data){
				if(selectBG.bitmapData) selectBG.bitmapData.dispose();
				selectBG.bitmapData = data;
				selectBG.x = (WIDTH_CELL - selectBG.width)/2;
				selectBG.y = (HEIGHT_CELL - selectBG.height)/2+2;
			}else{
				if(selectBG.bitmapData){
					selectBG.bitmapData.dispose();
				}
				selectBG.bitmapData = null;
				
				if(isIcon){
					
				}else{
					if(cellId < 10){
						var format:TextFormat = new TextFormat("宋体",12,0xffffff);
						tipTxt.defaultTextFormat = format;	
						tipTxt.text = cellTip;
						tipTxt.filters = null;
					}
				}
			}
		}
		
		public function clear():void{
			while(this.numChildren > 0){
				this.removeChildAt(0);
			}
			this.removeEventListener(MouseEvent.CLICK,clickHandler);
			cellTip = null;
			tipTxt = null;
			if(selectBG){
				if(selectBG.bitmapData != null){
					selectBG.bitmapData.dispose();
					selectBG.bitmapData = null;
				}
			}
			selectBG = null;
			cellId = 0;
			position = null;
			if(filterList){
				filterList.slice(0);
			}
			filterList = null;
			tipSprite = null;
			iconSprite = null;
		}
		
	}
}